﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor.IMGUI.Controls;
using UnityEngine.Rendering;

namespace NinjaToolbox.PhysicsSystem
{
    public class SphereShapeSceneHandleDrawer : ShapeSceneHandleDrawerBase
    {
        SphereShape mSphereShape;



        public override void Initialization(SerializedObject serializedObject, IPhysicsShape physicsShape)
        {
            mSphereShape = physicsShape as SphereShape;

            base.Initialization(serializedObject, physicsShape);
        }

        protected override void DrawSceneGUI(SceneView sceneView)
        {
            using (var changeCheck = new EditorGUI.ChangeCheckScope())
            {
                var scale = mSphereShape.matrix.lossyScale;
                var maxScale = Mathf.Max(Mathf.Max(Mathf.Abs(scale.x), Mathf.Abs(scale.y)), Mathf.Abs(scale.z));

                var defaultGizmosColor = PhysicsUtil.PHYSICS_SHAPE_GIZMOS_COLOR;

                var cacheHandlesColor = Handles.color;
                Handles.color = defaultGizmosColor;
                float newRadius = Handles.RadiusHandle(Quaternion.identity, mSphereShape.GetWorldCenter(), mSphereShape.radius * maxScale, true);
                Handles.color = cacheHandlesColor;

                if (changeCheck.changed)
                {
                    Undo.RecordObject(mSerializeObject.targetObject, "NinjaToolbox.PhysicsSystem.SphereShape");

                    mSphereShape.radius = newRadius;
                }
            }
        }
    }
}
